package com.atguigu.ssyx.common.exception;

import com.atguigu.ssyx.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author LiGuoCai
 * @Date 2023/10/16 1:10
 * @PackageName:com.atguigu.ssyx.common.exception
 * @Description: 全局异常处理器
 * @Version 1.0
 */
@ControllerAdvice
//添加@ResponseBody，因为之后捕获异常以后需要返回json形式的数据
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理所有的异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result error(Exception e) {
        e.printStackTrace();
        return Result.fail(null);
    }

    @ExceptionHandler(SsyxException.class)
    public Result error(SsyxException e) {
        return Result.build(null,e.getCode(), e.getMessage());
    }

}
